
var accessid= 'LTAIAEzlAVxo9QHx';
var accesskey= 'ceje0lXzUmtfHCLz2qPnRWAnPpQS84';
var host = 'http://sano100-tjb.oss-cn-beijing.aliyuncs.com';

var g_dirname = 'ac/dc/upload/'
var g_object_name = ''
var g_object_name_type = 'random_name'
var now = timestamp = Date.parse(new Date()) / 1000;

var policyText = {
    "expiration": "2020-01-01T12:00:00.000Z", //设置该Policy的失效时间，超过这个失效时间之后，就没有办法通过这个policy上传文件了
    "conditions": [
        ["content-length-range", 0, 1048576000] // 设置上传文件的大小限制
    ]
};

var policyBase64 = Base64.encode(JSON.stringify(policyText))
message = policyBase64
var bytes = Crypto.HMAC(Crypto.SHA1, message, accesskey, { asBytes: true }) ;
var signature = Crypto.util.bytesToBase64(bytes);


function chooseAndUploadToOss(func) {
    uexFileMgr.explorer("file:///sdcard/DCIM",function(err,path){
        if(!err){
            uploadOnePicToOss (path ,func)
        }else{
            showToast("程序出错,@x2")
        }
    });
}
function uploadOnePicToOss (path ,func) {

    //func("",path);
    //return false;


    log("pre upload1")
    //var param = {Filedata:{path:data}}


    var newFileName = random_string() + random_string() + get_suffix(path);
    var param = {
        'key' : g_dirname + newFileName,
        'policy': policyBase64,
        'OSSAccessKeyId': accessid,
        'success_action_status' : '200', //让服务端返回200,不然，默认会返回204
        'signature': signature,
        'file':{
            path:path
        }
    }
    log("pre upload2")
    log(param)

    $("#progress").show(100)
    $("#percent").css("width","0%")
    appcan.ajax({
        url : host,
        type : "POST",
        data :param,
        cache: false,//上传文件无需缓存
        processData: false,//用于对data参数进行序列化处理 这里必须false
        contentType: false, //必须
        success : function(data, status, requestCode, response, xhr) {
            if(requestCode == 200){
                //上传成功
                log(" upload OK")
                log(newFileName)
                log(path)
                log(" upload OK")
                var data = {d:host + "/" +g_dirname + newFileName}
                func({d:host + "/" +g_dirname + newFileName})
            }
            $("#progress").fadeOut(300)
        },
        progress : function(progress, xhr) {
            //log("loading:" + progress + "%")
            $("#percent").css("width",progress + "%")
        },
        error : function(xhr, erroType, error, msg) {
            showToast("上传时发生错误")
        }
    });
}



function random_string(){
    var len = 32;
    var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
    var maxPos = chars.length;
    var pwd = '';
    for (var i = 0;i<len;i++) {
        pwd += chars.charAt(Math.floor(Math.random() * maxPos));
    }
    return pwd;
}


function get_suffix(filename) {
    pos = filename.lastIndexOf('.')
    suffix = ''
    if (pos != -1) {
        suffix = filename.substring(pos)
    }
    return suffix;
}